package com.hshx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hshx.VO.JobClassVo;
import com.hshx.VO.PositionVo;
import com.hshx.pojo.Job_classification;
import com.hshx.pojo.Position;
import com.hshx.pojo.Users;
import com.hshx.service.IMessageProduceService;
import com.hshx.service.Job_classificationService;
import com.hshx.service.PositionService;
import com.hshx.service.UsersService;
import com.hshx.util.RedisUtil1;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin
@RequestMapping("/systemService")
@RestController
public class Job_classificationController {
    @Autowired
    private Job_classificationService jcs;
    @Autowired
    private PositionService positionService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private RedisUtil1 redisUtil1;
    @Autowired
    private IMessageProduceService service;

    /**
     * 添加一个职位分类
     * @param job_classification
     * @return
     */
    @RequestMapping("/addJob_classificationOne")
    public boolean addJob_classificationOne(@SpringQueryMap Job_classification job_classification){
        if(jcs.save(job_classification)){
            flushJobClassRedis();
            return true;
        }
        return false;
    }

    /**
     * 从数据库中移除一个职位分类
     * @param job_classification_id
     * @return
     */
    @RequestMapping("/removeJob_classification")
    public boolean removeJob_classification(int job_classification_id){
        if(positionService.count(new QueryWrapper<Position>().eq("position_job",job_classification_id))>0){
            return false;
        }
        if(jcs.removeById(job_classification_id)){
            flushJobClassRedis();
            return true;
        }
        return false;
    }

    /**
     * 修改某个职位分类信息
     * @param job_classification
     * @return
     */
    @RequestMapping("/updateJob_classification")
    public boolean updateJob_classification(@SpringQueryMap Job_classification job_classification){
        if(positionService.count(new QueryWrapper<Position>().eq("position_job",job_classification.getJob_classification_id()))>0){
            return false;
        }
        if(jcs.updateById(job_classification)){
            flushJobClassRedis();
            return true;
        }
        return false;
    }

    /**
     * 获取所有职位分类信息
     * @return
     */
    @RequestMapping("/getAllJob_classification")
    public List<Job_classification> getAllJob_classification(){
        return jcs.list();
    }

    /**
     * 获取所有启用的职位分类
     * @return
     */
    @RequestMapping("/getAllJobClass")
    public List<Job_classification> getAllJobClass(){
        List<Object> objects = redisUtil1.lRange("JobClassAll",0,-1);
        if(objects!=null&&objects.size()>0) {
            System.out.println("redis中存在");
        }
        return jcs.list(new QueryWrapper<Job_classification>().eq("job_classification_state",2));
    }
    /**
     * 获取某个分类信息
     * @param job_classification_id
     * @return
     */
    @RequestMapping("/getJob_classification")
    public Job_classification getJob_classification(int job_classification_id){
        return jcs.getById(job_classification_id);
    }

    /**
     * 职位分类表格查询
     * @return
     */
    @RequestMapping("/getJobClassPage")
    public Map<String,Object> getJobClassPage(Integer page, Integer limit){
        HashMap<String, Object> jobClassMap = new HashMap<>();
        if(page==null||page<=0){
            page=1;
        }
        if(limit==null||limit<=0){
            limit=10;
        }
        List<Object> objects = redisUtil1.lRange("JobClassAll",0,-1);
        if(objects!=null&&objects.size()>0){
            System.out.println("redis中存在");

            List<Job_classification> JobClassList= objects.stream().map(o->((Job_classification)o)).collect(Collectors.toList());
            jobClassMap.put("code", 0);
            jobClassMap.put("limit",limit);
            jobClassMap.put("pages", JobClassList.size()%limit==0?JobClassList.size()/limit:JobClassList.size()/limit+1);
            jobClassMap.put("pageNum",page);
            jobClassMap.put("data", JobClassList.stream().sorted(Comparator.comparing(Job_classification::getJob_classification_id).reversed()).skip((page-1)*limit).limit(limit).collect(Collectors.toList()));
            jobClassMap.put("count", JobClassList.size());
        }else{//redis中不存在或失效
            System.out.println("redis中不存在或失效");

//            Page jobClassPage= PageHelper.startPage(page,limit);
            List<Job_classification> jobClassList=jcs.list(new QueryWrapper<Job_classification>().orderByDesc("job_classification_id"));
            redisUtil1.lPushAll("JobClassAll",jobClassList,24*60L);
            jobClassMap.put("code", 0);
            jobClassMap.put("limit",limit);
//            jobClassMap.put("pages", jobClassPage.getPages());
//            jobClassMap.put("pageNum",jobClassPage.getPageNum());
            jobClassMap.put("data",jobClassList.stream().sorted(Comparator.comparing(Job_classification::getJob_classification_id).reversed()).skip((page-1)*limit).limit(limit).collect(Collectors.toList()));
            jobClassMap.put("count", jobClassList.size());
        }
        return jobClassMap;
    }

    /**
     * 获取所有启用的职位分类信息
     * @return
     */
    @RequestMapping("/getJobClassList")
    public List<Job_classification> getJobClassList(){
        List<Object> objects = redisUtil1.lRange("jobClassList",0,-1);
        if(objects!=null&&objects.size()>0){
            System.out.println("redis中存在");
            return objects.stream().map(o->((Job_classification)o)).collect(Collectors.toList());
        }
        List<Job_classification> jobClassList=jcs.list(new QueryWrapper<Job_classification>().eq("job_classification_state",1));
        redisUtil1.lPushAll("jobClassList",jobClassList,24*60L);
        return jobClassList;
    }

    @RequestMapping("/hasJobClassName")
    public boolean hasJobClassName(String job_classification_name){
        return jcs.count(new QueryWrapper<Job_classification>().eq("job_classification_name",job_classification_name))==0;
    }

    @RequestMapping("/getJobClassVoList")
    public List<JobClassVo> getJobClassVoList(){
        List<Job_classification> jobClassList = jcs.list(new QueryWrapper<Job_classification>().eq("Job_classification_state", 1));
        List<JobClassVo> jobClassVoList = new ArrayList<>();
        for(Job_classification jobClass:jobClassList){
            JobClassVo jobClassVo=new JobClassVo();
            jobClassVo.setJobClassName(jobClass.getJob_classification_name());

            List<Position> positionList=positionService.list(new QueryWrapper<Position>().eq("position_job",jobClass.getJob_classification_id()));
            if(positionList==null||positionList.size()==0){
                jobClassVo.setJobClassNum(0L);
            }

            long total=0;
            for(Position position:positionList){
                long num=usersService.count(new QueryWrapper<Users>().eq("users_position",position.getPosition_id()));
                total+=num;
            }
            jobClassVo.setJobClassNum(total);
            jobClassVoList.add(jobClassVo);

        }
        return jobClassVoList;
    }


    /**
     * 更新所有职位分类的信息
     */
    private void flushJobClassRedis(){
        redisUtil1.remove("jobClassList");
        redisUtil1.lPushAll("jobClassList",jcs.list(new QueryWrapper<Job_classification>().eq("job_classification_state",1)),60*24L);
        redisUtil1.remove("JobClassAll");
        redisUtil1.lPushAll("JobClassAll",jcs.list(new QueryWrapper<Job_classification>().orderByDesc("job_classification_id")),60*24L);
    }

}
